package com.ljkj.zjf.rest;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ljkj.access.Tright;
import com.ljkj.access.Tuser;
import com.ljkj.common.application.SpringApplicationContext;
import com.ljkj.common.db.util.JSONUTIL;
import com.ljkj.zjf.common.CacheManager;
import com.ljkj.zjf.common.GlobalNames;
import com.ljkj.zjf.service.TRightService;
import com.ljkj.zjf.service.TuserRightService;
import com.ljkj.zjf.service.TuserService;

@Path("rest/right")
public class RightSource {
	
	private static TuserRightService tuserRightService;
	private static TRightService tRightService;
	private static TuserService tuserService;
	
	//初始化service
	static {
		tuserRightService  = (TuserRightService) SpringApplicationContext.getBean("tuserRightService");
		tRightService = (TRightService) SpringApplicationContext.getBean("tRightService");
		tuserService  = (TuserService) SpringApplicationContext.getBean("tuserService");
	}
	
	private static Logger logger = LoggerFactory.getLogger(RightSource.class);
	
	@Path("/rightByUser")
	@POST
	@Produces("application/json; charset=utf-8")
	public String rightJsonByUser(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");

		JSONObject json_object = JSON.parseObject(decode_str);
		String userId = json_object.getString("userId"); 
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String matchRes = CacheManager.getSession().getIfPresent(userId);
		if (null == matchRes || "".equals(matchRes)) {
			errorMap.put("errMsg", "您尚未登录，请登录再操作。");
			return JSON.toJSONString(errorMap);
		}
		
		String result = this.tuserRightService.getUserRightByUserID(Integer.valueOf(userId));
		System.out.println(result);
		return result;
		
	}
	
	@Path("/rightByUserID")
	@POST
	@Produces("application/json; charset=utf-8")
	public String rightJsonByUserID(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");

		JSONObject json_object = JSON.parseObject(decode_str);
		String userId = json_object.getString("userId"); 
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String result = this.tuserRightService.getUserRightByUserID(Integer.valueOf(userId));
		System.out.println(result);
		return result;
		
	}
	
	
	@Path("/right_list")
	@POST
	@Produces("application/json; charset=utf-8")
	public String rightListData(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		//pageIndex=0&pageSize=10&sortField=&sortOrder=
		String params = json_object.getString("params");
		String pageIndex = json_object.getString("pageIndex");
		int currentPage = 1;
		if(pageIndex != null && !"".equals(pageIndex)){
			currentPage = Integer.parseInt(pageIndex)+1;
		}
		String pageSize_str = json_object.getString("pageSize");
		int pageSize = 10;
		if(pageSize_str != null && !"".equals(pageSize_str)){
			pageSize = Integer.parseInt(pageSize_str);
		}
		String sortField = json_object.getString("sortField");
		String sortOrder = json_object.getString("sortOrder");
		
		String userId = json_object.getString("userId"); 
		if (userId != null || !"".equals(userId)) {
			String result = this.tRightService.getRightByProperties(params, currentPage, pageSize, sortField, sortOrder);
			return result;
		}
		
		return null;
		
	}
	
	@Path("/parent_list")
	@GET
	@Produces("application/json; charset=utf-8")
	public String rightParentData() throws Exception {
		
		String result = this.tRightService.getParmentRight();
		return result;
		
	}
	
	@Path("/for_right_list")
	@POST
	@Produces("application/json; charset=utf-8")
	public String rightForEditUser(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		String userId = json_object.getString("userId"); 
		String right_userId = json_object.getString("right_userId");
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String matchRes = CacheManager.getSession().getIfPresent(userId);
		if (null == matchRes || "".equals(matchRes)) {
			errorMap.put("errMsg", "您尚未登录，请登录再操作。");
			return JSON.toJSONString(errorMap);
		}
		
		Integer rightUserId = null;
		if(right_userId != null && !"".equals(right_userId)){
			rightUserId = Integer.valueOf(right_userId);
		}
		
		String result = this.tRightService.getParmentRight();
		return result;
		
	}
	
	@Path("/right_info")
	@POST
	@Produces("application/json; charset=utf-8")
	public String addOrEditRightInfo(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		String userId = json_object.getString("userId"); 
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String rightId_temp = json_object.getString("rightId");
		Tright tright = null;
		Integer rightId = null;
		if(rightId_temp != null && !"".equals(rightId_temp)){
			rightId = Integer.parseInt(rightId_temp);
			tright = this.tRightService.getTrightById(rightId);
		}
		if(tright == null){
			tright = new Tright();
		}
		
		String rightName = json_object.getString("rightName");
		if(rightName!= null){
			tright.setRightName(rightName);
		}
		String description = json_object.getString("description");
		if(description!= null){
			tright.setDescription(description);
		}
		String rightUrl = json_object.getString("rightUrl");
		if(rightUrl!= null){
			tright.setTrUrl(rightUrl);
		}
		String parentId_temp = json_object.getString("parentId");
		Integer parentId = null;
		if(parentId_temp != null && !"".equals(parentId_temp)){
			parentId = Integer.parseInt(parentId_temp);
			tright.setParentTrId(parentId);
		}
		
		Integer disabledState = null;
		String disabledState_temp = json_object.getString("disabledState");
		if(disabledState_temp != null && !"".equals(disabledState_temp)){
			disabledState = Integer.parseInt(disabledState_temp);
			tright.setDisabledState(disabledState);
		}
		
		Tuser tuser = this.tuserService.getUserById(Integer.valueOf(userId));
		String result = this.tRightService.addOrEditRightInfo(tright,tuser);
		return result;
		
	}


}
